package com.hm.system.auto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hm.email.share.service.SendEmailService;
import com.hm.recycle.share.service.DbDocumentDeleteRecycleService;
import com.hm.recycle.share.service.DbDocumentNewRecycleService;
import com.hm.recycle.share.service.DbDocumentUpdateRecycleService;
import com.hm.system.share.service.mongo.log.RequestPlanLogService;
import com.hm.system.share.service.mongo.system.SysUserInfoService;

/**
 * @author shishun.wang
 * @date 下午2:26:16 2016年7月5日
 * @version 1.0
 * @describe
 */
@Configuration
@EnableConfigurationProperties({ SystemProperties.class })
public class SystemAutoConfiguration {

	@Autowired
	private SystemProperties systemProperties;

	@Autowired
	private BeanFactory beanFactory;
	
	@Bean
	public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
		ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
		{
			threadPoolTaskExecutor.setQueueCapacity(100);
			threadPoolTaskExecutor.setCorePoolSize(3);
			threadPoolTaskExecutor.setMaxPoolSize(50);
			threadPoolTaskExecutor.setKeepAliveSeconds(30000);
			threadPoolTaskExecutor.initialize();
		}
		
		return threadPoolTaskExecutor;
	}

	@Bean
	public DbDocumentDeleteRecycleService dbDocumentDeleteRecycleService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemProperties.getHttpInvokerService().getRecycle()
				+ DbDocumentDeleteRecycleService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(DbDocumentDeleteRecycleService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (DbDocumentDeleteRecycleService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public DbDocumentUpdateRecycleService dbDocumentUpdateRecycleService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemProperties.getHttpInvokerService().getRecycle()
				+ DbDocumentUpdateRecycleService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(DbDocumentUpdateRecycleService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (DbDocumentUpdateRecycleService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public DbDocumentNewRecycleService dbDocumentNewRecycleService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemProperties.getHttpInvokerService().getRecycle()
				+ DbDocumentNewRecycleService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(DbDocumentNewRecycleService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (DbDocumentNewRecycleService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public SendEmailService sendEmailService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(
				systemProperties.getHttpInvokerService().getEmail() + SendEmailService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(SendEmailService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (SendEmailService) httpInvokerProxyFactoryBean.getObject();
	}
	
	@Bean(name = RequestPlanLogService.HTTP_INVOKER_EXPORT_CONTEXT)
	public HttpInvokerServiceExporter requestPlanLogService() {
		HttpInvokerServiceExporter httpInvokerServiceExporter = new HttpInvokerServiceExporter();
		httpInvokerServiceExporter.setServiceInterface(RequestPlanLogService.class);
		httpInvokerServiceExporter.setService(beanFactory.getBean(RequestPlanLogService.class));
		httpInvokerServiceExporter.afterPropertiesSet();
		return httpInvokerServiceExporter;
	}
	
	@Bean(name = SysUserInfoService.HTTP_INVOKER_EXPORT_CONTEXT)
	public HttpInvokerServiceExporter sysUserInfoService() {
		HttpInvokerServiceExporter httpInvokerServiceExporter = new HttpInvokerServiceExporter();
		httpInvokerServiceExporter.setServiceInterface(SysUserInfoService.class);
		httpInvokerServiceExporter.setService(beanFactory.getBean(SysUserInfoService.class));
		httpInvokerServiceExporter.afterPropertiesSet();
		return httpInvokerServiceExporter;
	}
	
	/*@Bean
	public FilterRegistrationBean corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		CorsConfiguration config = new CorsConfiguration();
		config.setAllowCredentials(true);
		config.addAllowedOrigin("*");
		config.addAllowedHeader("*");
		config.addAllowedMethod("OPTIONS");
		config.addAllowedMethod("HEAD");
		config.addAllowedMethod("GET");
		config.addAllowedMethod("PUT");
		config.addAllowedMethod("POST");
		config.addAllowedMethod("DELETE");
		config.addAllowedMethod("PATCH");
		source.registerCorsConfiguration("/**", config);
		final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
		bean.setOrder(0);
		return bean;
	}*/
	
	@Bean
	@ConditionalOnMissingBean
	public ObjectMapper objectMapper() {
		return new ObjectMapper();
	}
}
